package com.m3958.firstgwt.server.model;

import java.util.Date;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import com.m3958.firstgwt.server.types.JsonExcludeFields;
import com.m3958.firstgwt.server.utils.JsonlibDateValueProssor;

@Entity
@Table(name="ASYNC_TASK")
@AttributeOverride(name="id",column=@Column(nullable = false,name="ASYNC_TASK_ID"))
public class AsyncTask extends BaseModel<AsyncTask> implements HasCreator{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static enum TaskCat{
		CLONE_PAGE,BIND_USER
	}
	
	public static enum TaskStatus{
		UNTOUCHED,COMMITTED,SUCCESS,FAILURE
	}
	
	
	@Override
	public JSONObject toJson(){
		JsonConfig jc = new JsonConfig();
		jc.registerJsonValueProcessor(Date.class, injector.getInstance(JsonlibDateValueProssor.class));
		jc.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		jc.setExcludes(JsonExcludeFields.ASYNC_TASK_EXCLUDES);
		return JSONObject.fromObject(this, jc);
	}

	
	private String name;
	
	private String creatorIds;
	
	private String catagory;
	
	@Lob
	private String taskDescript;
	
	@Lob
	private String errorMsg;
	
	private String status;
	
	private boolean notified;
	
	@ManyToOne
	@JoinColumn(name="CREATOR_ID",referencedColumnName="USER_ID")
	protected User creator;
	
	
	@Override
	public User getCreator() {
		return creator;
	}

	@Override
	public void setCreator(User creator) {
		this.creator = creator;
		creatorIds = getCreatorPath(creator);
	}

	@Override
	public String getCreatorIds() {
		return creatorIds;
	}

	public void setTaskDescript(String taskDescript) {
		this.taskDescript = taskDescript;
	}

	public String getTaskDescript() {
		return taskDescript;
	}


	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}


	public void setNotified(boolean notified) {
		this.notified = notified;
	}

	public boolean isNotified() {
		return notified;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public String getErrorMsg() {
		return errorMsg;
	}
	
	public void setStatus(String status) {
		this.status = status;
	}

	public String getStatus() {
		return status;
	}

	public void setCatagory(String catagory) {
		this.catagory = catagory;
	}

	public String getCatagory() {
		return catagory;
	}

	@Override
	public String[] getJsonExcludes() {
		// TODO Auto-generated method stub
		return null;
	}

}
